package com.mycompany.Test.server;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.mycompany.Test.shared.*;


public class Parsage {
	
	public static String convertXMLFileToString(String fileName) 
    { 
      try{ 
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
        InputStream inputStream = new FileInputStream(new File(fileName)); 
        org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); 
        StringWriter stw = new StringWriter(); 
        Transformer serializer = TransformerFactory.newInstance().newTransformer(); 
        serializer.transform(new DOMSource(doc), new StreamResult(stw)); 
        return stw.toString(); 
      } 
      catch (Exception e) { 
        e.printStackTrace(); 
      } 
        return null; 
    }
	
	public static AllObjects XML(String file, List<Status> switcheslist,List<Sensor> sensorslist,List<Status> detectorslist,List<Variator> variatorslist) throws ParserConfigurationException, SAXException, IOException{
		

		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document dom = builder.parse(file);
	
	    Element racine = (Element)dom.getDocumentElement();

		// RÃ©cupÃ©ration des diffÃ©rents Ã©lÃ©ments
		
		Element switches = (Element)dom.getElementsByTagName("Switches").item(0);
		Element sensors = (Element)dom.getElementsByTagName("Sensors").item(0);
		Element detectors = (Element)dom.getElementsByTagName("Detectors").item(0);
		Element variators = (Element)dom.getElementsByTagName("Variators").item(0);

		

		NodeList switchesnodelist = switches.getChildNodes();
		NodeList sensorsnodelist = sensors.getChildNodes();
		NodeList detectorsnodelist = detectors.getChildNodes();
		NodeList variatorsnodelist = variators.getChildNodes();


		// On s'occupe que des switches pour cette boucle
		for (int i = 0; i < switchesnodelist.getLength(); i++) {


			Node nx = switchesnodelist.item(i);
			
			if(nx instanceof Element){
				Element next = (Element) nx;
				String name = next.getTagName();
				String id = next.getAttribute("id");
				String location = next.getAttribute("location");
				if (next.getAttributeNode("state") != null){
					String state = next.getAttribute("state");
					Status s= new Status(name,id,location,"NULL",state);
					switcheslist.add(s);
				}
				else {
					Status s= new Status(name,id,location,"NULL","NULL");
					switcheslist.add(s);
				}

			}
		}

		// On s'occupe que des sensors pour cette boucle
		for (int i = 0; i < sensorsnodelist.getLength(); i++) {


			Node nx = sensorsnodelist.item(i);
			if(nx instanceof Element){
				Element next = (Element) nx;
				String name = next.getTagName();
				String id = next.getAttribute("id");
				String location = next.getAttribute("location");
				Sensor s= new Sensor(name,id,location,"NULL","NULL");
				sensorslist.add(s);
			}					
		}

		// On s'occupe que des detectors pour cette boucle
		for (int i = 0; i < detectorsnodelist.getLength(); i++) {


			Node nx = detectorsnodelist.item(i);
			if(nx instanceof Element){
				Element next = (Element) nx;
				String name = next.getTagName();
				String id = next.getAttribute("id"); 
				String location = next.getAttribute("location");

				Status s= new Status(name, id,location,"NULL","NULL");
				detectorslist.add(s);						
			}
		}

		// On s'occupe que des variators pour cette boucle
		for (int i = 0; i < variatorsnodelist.getLength(); i++) {


			Node nx = variatorsnodelist.item(i);
			if(nx instanceof Element){
				Element next = (Element) nx;
				String name = next.getTagName();
				String id = next.getAttribute("id"); 
				String location = next.getAttribute("location");

				Variator s= new Variator(name, id,location,"NULL","NULL","NULL");
				variatorslist.add(s);	
			}

		}

		AllObjects allobjects = new AllObjects(switcheslist,sensorslist,detectorslist,variatorslist);
		return allobjects;
	}


public static AllObjects XMLState(String file, List<Status> switcheslist) throws ParserConfigurationException, SAXException, IOException{
	

	DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	Document dom = builder.parse(file);

    Element racine = (Element)dom.getDocumentElement();

	// RÃ©cupÃ©ration des diffÃ©rents Ã©lÃ©ments
	
	Element switches = (Element)dom.getElementsByTagName("Switches").item(0);
	

	NodeList switchesnodelist = switches.getChildNodes();



	// On s'occupe que des switches pour cette boucle
	for (int i = 0; i < switchesnodelist.getLength(); i++) {


		Node nx = switchesnodelist.item(i);
		
		if(nx instanceof Element){
			Element next = (Element) nx;
			String name = next.getTagName();
			String id = next.getAttribute("id");
			String state = next.getAttribute("state");
			Status s= new Status(name,id,"NULL","NULL",state);
			switcheslist.add(s);
		}
	}


	AllObjects allobjects = new AllObjects(switcheslist,null,null,null);
	return allobjects;
}
}
